import { ajax , versionCompare } from './common.js';
const appUpgrade = (opt)=>{
	opt = opt || {};
	let platform = uni.getStorageSync('platform')
	opt.success = opt.success || function() {};
	setTimeout(()=>{
		ajax({
			url: 'appupdate',
			data: {
				platform: platform
			},
			loading: 2,
			success: (res) => {
				if (res.data.code == 0) {
					let data = res.data.data;
					let version_code = uni.getStorageSync('version_code');
					if (versionCompare(plus.runtime.version,data.version) || data.version_code > version_code) {
						if (data.type == 1 && versionCompare(plus.runtime.version,data.version)) {
							opt.success(data)
							return false;
						} else if (data.type == 3 && data.version_code > version_code) {
							let dtask = plus.downloader.createDownload(data.download_url, {}, function(d, status) {
								if (status == 200) {
									plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), { force: true }, function(res) {
										uni.setStorageSync('version_code', data.version_code);
									});
								}
							});
							dtask.start();
						}
					}
				}
			}
		});
	},300)
}
export {
	appUpgrade
}